package cn.edu.scau.cmi.xuminbang.client.classicsDesignPattern;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.xuminbang.adapter.AdapterOfClass;
import cn.edu.scau.cmi.xuminbang.adapter.AdapterOfObject;
import cn.edu.scau.cmi.xuminbang.adapter.AdapterStudent;
import cn.edu.scau.cmi.xuminbang.adapter.Students;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class AdapterClient extends Application implements Initializable{
	@FXML private Button submit;
	@FXML private TextField num;
	@FXML private TextField name;
	@FXML private Button sortnum;
	@FXML private TextArea show1;
	@FXML private TextArea show2; 

	public void start(Stage primaryStage) throws Exception {
		Stage stage = new Stage();
		stage = primaryStage;
        try{
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("/cn/edu/scau/cmi/xuminbang/fxml/AdapterClient.fxml"));
            Parent root = (Parent)loader.load();
            Scene scene = new Scene(root);
            stage.setTitle("Adapter");
            stage.setScene(scene);
            stage.show();
            Students.stuList=new ArrayList<AdapterStudent>();
        }catch (Exception e){
            e.printStackTrace();
        }
	
	}
	public static void main(String[] args) {
        launch(args);
    }

	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		
	}
	public void submitStudent() {
		
		
		Students.stuList.add(new AdapterStudent(Integer.parseInt(num.getText()), name.getText()));
		
		num.clear();
		name.clear();
	}
	public void sortPlay() {
		show1.clear();
		show2.clear();
		int m=0;int n=0;
		AdapterOfClass adapter = new AdapterOfClass();
		ArrayList<Integer> tempnum = null;	
		tempnum = adapter.show();
		for(m=0;m<tempnum.size();m++) {
			for(n=0;n<Students.stuList.size();n++) {
				if(Students.stuList.get(n).num==tempnum.get(m)) {
					show1.appendText(Students.stuList.get(n).num+Students.stuList.get(n).name+"\n");
					//show2.appendText(Students.stuList.get(n).num+Students.stuList.get(n).name+"\n");
				}
			}
			
		}
		AdapterOfObject adapterOfObject = new AdapterOfObject();
		tempnum.clear();
		tempnum=adapterOfObject.instance.sort(adapter.getInt());
		for(m=0;m<tempnum.size();m++) {
			for(n=0;n<Students.stuList.size();n++) {
				if(Students.stuList.get(n).num==tempnum.get(m)) {
					//show1.appendText(Students.stuList.get(n).num+Students.stuList.get(n).name+"\n");
					show2.appendText(Students.stuList.get(n).num+Students.stuList.get(n).name+"\n");
				}
			}
			
		}
		
	}

}
